package com.webchat.client.mall.controller;


import com.webchat.client.mall.service.AccountService;
import com.webchat.client.mall.service.UserSessionService;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.ValidateLogin;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mall-service/account")
public class AccountController {

    @Autowired
    private AccountService userService;

    @Autowired
    private UserSessionService userSessionService;

    @Autowired
    private HttpServletRequest request;

    @Value("${oauth.server-url}")
    private String logoutServer;

    /**
     * 查询电商服务登录用户信息
     *
     * @return
     */
    @GetMapping("/current/info")
    public APIResponseBean<UserBaseResponseInfoVO> getCurrentUserInfo() {
        String userId = SessionHelper.getCurrentUserId();
        return APIResponseBeanUtil.success(userService.userInfo(userId));
    }

    @ValidateLogin
    @GetMapping("/current/login/user")
    public APIResponseBean<UserBaseResponseInfoVO> getLoginUser() {
        String userId = SessionHelper.getCurrentUserId();
        return APIResponseBeanUtil.success(userService.userInfo(userId));
    }

    /**
     * 退出登录
     *
     * @return
     */
    @GetMapping("/logout")
    public APIResponseBean<String> logout() {
        userSessionService.logout(request);
        String ssoServerUrl = String.format(logoutServer, request.getHeader("origin-url"));
        return APIResponseBeanUtil.success(ssoServerUrl);
    }

}
